tinymce.create('tinymce.plugins.popupLinkPlugin', {
    init : function(ed, url) {
        // Register commands
        
        //lookup pagelink manager url
        ed.addCommand('popupLinkPlugin', function() {
            var e = ed.selection.getNode();

            // Internal image object like a flash placeholder
            if (ed.dom.getAttrib(e, 'class').indexOf('mceItem') != -1)
                return;

            var popupLinkCallback = ed.getParam("popup_link_chooser_callback");

            var callback = function(selectedValue){
                var selected = tinyMCE.selectedInstance.selection.getContent();

                var rollover = "<a class='contentTooltip' onmouseover=\"startTooltip(event, '" + selectedValue +
                               "')\" onmouseout=\"stopTooltip()\">" + selected + "<\a>";

                if (selected.length > 0){
                    tinyMCE.execCommand("mceInsertRawHTML", false, rollover);
                }
            };

            if (eval('typeof(' + popupLinkCallback + ')') == "undefined")
                alert("Callback function: " + popupLinkCallback + " could not be found.");
            else
                eval(popupLinkCallback + "(" + callback.toString() + ")");
        });

        // Register buttons
        //tinyMCE.getButtonHTML(cn, 'lang_imgmap_desc', '{$pluginurl}/images/tinymce_button.gif', 'mceimgmapPopup');
        ed.addButton('popupLink', {
            title : 'Popup Link',
            cmd : 'popupLinkPlugin',
            image : url + '/images/icon.gif'
        });

        // Add a node change handler, selects the button in the UI when a image is selected
        ed.onNodeChange.add(function(ed, cm, n, co) {
            cm.setDisabled('popupLink', co && n.nodeName != 'A');
            //todo:detect popup 
            //cm.setActive('popupLink', n.nodeName == 'A' && !n.name);
        });

    },

    getInfo : function() {
        return {
            longname  : 'Braintrain Popup Link Plugin',
            author    : 'John Ericksen',
            version   : "1.0"
        };
    }
});

// Register plugin
tinymce.PluginManager.add('popupLink', tinymce.plugins.popupLinkPlugin);
